package copyengine.ui.infoTree.loadTexture
{
    import copyengine.infoTree.meta.CESFileMeta;
    import copyengine.log.CELog;
    import copyengine.resource.CEResUtils;
    import copyengine.resource.resInfo.CEResInfoManger;
    import copyengine.resource.resInfo.MetaEachResFileInfo;
    import copyengine.resource.resProxy.CEResFIleDesc;
    import copyengine.resource.resProxy.CEResProxy;
    import copyengine.resource.resProxy.loadQueue.CEResLoadQueue;
    import copyengine.utils.CECallbackFunction;
    import copyengine.utils.CEMemoryHolder;
    import copyengine.utils.CEStringUtils;

    public class InfoTreeBuilderLoadTextureHelper
    {
        private var mFinishCallback:CECallbackFunction;
        private var mFileMetaName:String;

        private var mResProxy:CEResProxy;

        public function InfoTreeBuilderLoadTextureHelper()
        {
        }

        public function loadCEFileAndWillUseTexture(_fileMetaName:String, _resProxy:CEResProxy, _finishCallback:CECallbackFunction):void
        {
            mResProxy = _resProxy;
            mFinishCallback = _finishCallback;
            mFileMetaName = _fileMetaName;
            CEMemoryHolder.instance.holdItem(this);

            //======================//
            //==  加载CESFileMeta 描述文件
            //======================//
            var resQueue:CEResLoadQueue = new CEResLoadQueue(onCEFileLoadComplete);

            var ceFileInfo:MetaEachResFileInfo = CEResInfoManger.instance.getFileInfoByFileName(_fileMetaName);
            var fileDesc:CEResFIleDesc = new CEResFIleDesc();
            fileDesc.fileName = ceFileInfo.name;
            fileDesc.filePath = ceFileInfo.path;
            fileDesc.fileSize = ceFileInfo.size;
            fileDesc.fileType = CEResFIleDesc.FILE_TYPE_COMPRESSED_OBJ;
            resQueue.pushNeedLoadRes(fileDesc);

            _resProxy.loadResQueue(resQueue);
        }

        private function onCEFileLoadComplete():void
        {
            var ceFile:CESFileMeta = CEResUtils.getDecompressedObj(mFileMetaName, mResProxy);

            if (ceFile.isShareTexture) {
                //只有使用ShareTexture的描述文件才可以直接加载所有需要的纹理信息,独立的描述信息 一下加载所有的纹理 没有意义
                var allNeededAtlasArray:Array = ceFile.needAtlasNameDic[ceFile.fileName];

                if (allNeededAtlasArray.length > 0) {

                    var resQueue:CEResLoadQueue = new CEResLoadQueue(finish);

                    for each(var needLoadAtlasName:String in allNeededAtlasArray) {

                        //===================//
                        //== 注意! 目前假设所有的atlas 结尾为xml,对应的纹理图为同名文件 结尾为png
                        //== 当后续使用ATF 或者atlas与png不同名时候再更改加载逻辑
                        //====================
                        var atlasFileInfo:MetaEachResFileInfo = CEResInfoManger.instance.getFileInfoByFileName(needLoadAtlasName);
                        var pngFileInfo:MetaEachResFileInfo = CEResInfoManger.instance.getFileInfoByFileName(
                                CEStringUtils.beforeLast(needLoadAtlasName, ".xml") + ".png");

                        var fileDesc:CEResFIleDesc = new CEResFIleDesc();
                        fileDesc.fileName = pngFileInfo.name;
                        fileDesc.filePath = pngFileInfo.path;
                        fileDesc.fileSize = pngFileInfo.size;
                        fileDesc.fileType = CEResFIleDesc.FILE_TYPE_ATLAS_TEXTURE;
                        fileDesc.extData = {};
                        fileDesc.extData[CEResFIleDesc.EXT_ATLAS_XML_FILE_PATH] = atlasFileInfo.path;

                        resQueue.pushNeedLoadRes(fileDesc);
                    }
                    mResProxy.loadResQueue(resQueue);
                } else {
                    //当前ceFile不需要加载任何Atlas
                    finish();
                }
            } else {
                CELog.err("Call InfoTreeBuilderLoadTextureHelper->loadCEFileAndWillUseTexture" +
                          " but file : " + mFileMetaName + " not use share texture");
            }
        }

        //=======================//
        //== Finish
        //=======================//

        private function finish():void
        {
            mFinishCallback.isEnable && mFinishCallback.callback();
            CEMemoryHolder.instance.releaseItem(this);
            mFinishCallback = null;
            mResProxy = null;
        }

    }
}
